[TcaplusDB]tcapulogmgr工具
tcapulogmgr主要用于对binlog进行操作,在数据回档时,如果需要回档到精确时间点,就需要使用该工具来达到目的。进入工具所在目录,执行 ./tcapulogmgr -h 即可获得各个子命令的介绍与用法,tcapulogmgr工具支持以下命令:
- query
- listfile
- restorealldatafiles
- updatefields
注意:
- 在使用工具时,工具的版本必须和svr的版本一致
- 在复制或备份引擎文件时,必须先正确停止对应的svr进程
- 在使用工具对引擎文件或ulog文件进行操作前,请注意备份
- 建议将命令需要的文件与工具置于同一目录下,或使用绝对路径的方式指定文件
1. query
query命令用于查询binlog流水,命令格式为 ./tcapulogmgr query -d dirtory -f file -b tbus_id [-a app_id -z zone_id -t table_name -s shard_id --keyonly --startseq startseq --endseq endseq --starttime starttime --endtime endtime],其中,前三个必填参数的含义分别为:
- -d 表示binlog文件所在的目录
- -f 表示要查询的binlog文件
- -b 表示tbus id,实际上就是生成该binlog文件的tcapsvr进程的进程id, 如1.2.2.1
方括号中的可选参数可用于过滤,其含义如下:
- -a 表示业务id
- -z 表示区服id
- -t 表示数据表名
- -s 表示shard id
- --keyonly 表示只查询key字段
- --startseq 表示起始的binlog记录序列号
- --endseq 表示结束的binlog记录序列号
- --starttime 表示binlog记录起始时间,格式为YYYYMMDDHHMMSS
- --endtime 表示binlog记录结束时间,格式为YYYYMMDDHHMMSS
需要注意的是,如果要根据业务id,区服id,数据表名和shard id来过滤的话,那么这个四个参数都必须给出,否则无法按要求进行过滤。
示例:./tcapulogmgr query -d /data/tcaplus/data/ulog/1.2.2.1 -f tcapsvr_1.2.2.1_00000661.ulog -b 1.2.2.1 -a 2 -z 3 -t table_generic -s 304
2. listfile
listfile命令用于输出binlog的文件信息,包括文件名,最小序列号,最大序列号等,命令格式为 ./tcapulogmgr listfile -d directory -b tbus_id [--startseq startseq --endseq endseq],各个参数的含义与query命令相同。
示例:./tcapulogmgr listfile -d /data/tcaplus/data/ulog/1.2.2.1 -b 1.2.2.1
3. restorealldatafiles
restorealldatafiles命令用于将指定svr上的所有数据回档到指定时间点,通常适用于使用冷备文件与binlog进行回档的场景。命令格式为 ./tcapulogmgr restorealldatafiles --directory= --tbusid= --engine-directory= [--endtime= --seqcontinuous],其中,前三个必填参数的含义分别为:
- --directory 表示ulog所在的目录
- --tbusid 表示要执行回档操作的svr_id,用于从directory中过滤出对应的ulog文件
- --engine-directory 表示冷备文件存放的目录
方括号中的参数为可选项:
- --endtime 表示要回档到的时间点,格式为YYYYMMDDHHMMSSmmm,mmm表示毫秒
- --seqcontinuous 表示binlog的序号必须连续,不连续则报错
示例:./tcapulogmgr restorealldatafiles --directory=/data1/ulog/1.2.2.1/ --tbusid=1.2.2.1 --endtime=20201201163000000 --engine-directory=/txh/ --seqcontinuous
4. updatefields
updatefields命令用于回档与动态变更表结构,命令格式为 ./tcapulogmgr updatefields --directory= --engine-directory= --tbusid= --conf= [--speed= --seqcontinuous],其中,前四个必填参数的含义分别为:
- --directory ulog文件目录
- --engine-directory 冷备文件目录
- --tbusid 表示要执行回档操作和动态变更表结构的svr_id,用于从目录中过滤出对应的binlog文件
- --conf updatefields命令的配置文件,指定了DirServerAddr、PassWord、要回档数据和导入数据的表以及用于转换记录的so文件
updatefields命令的配置文件模板与说明
<?xml version="1.0" encoding="GBK" standalone="yes" ?>
<tcapulogmgr>
<!--动态变更表结构的配置-->
<UpdateFieldsCfg UpdateFieldsCfgNum="1">
<!--SourceAppID、SourceZoneID、SourceTableName用于指定要回档的数据表的业务ID、区服ID和表名-->
<!--DestAppID、DestZoneID、DestTableName用于指定保存转换后的记录的业务ID、区服ID和表名-->
<!--LibFile用于指定用户生成的用来转换记录结构的so文件-->
<UpdateFieldsCfgItem SourceAppID="2" SourceZoneID="3" SourceTableName="TestData" DestAppID="2" DestZoneID="3" DestTableName="TestData_new" LibFile="do_nothing.so"/>
<!--PassWord用于指定DestTableName所在业务的密码-->
<ServiceApiCfg PassWord="**********" ConnectTimeOut="10000" RegistTableTimeOut="10000" DirServerNum="1" >
<!--DirServerAddr用于指定DestTableName所在业务的目录服务器地址-->
<DirServerAddr>tcp://*.*.*.*:9999</DirServerAddr>
</ServiceApiCfg>
</UpdateFieldsCfg>
</tcapulogmgr>
方括号中的参数为可选项:
- --speed 速度控制参数
- --seqcontinuous 表示binlog的序号必须连续,不连续则报错
假如用于转换记录结构的代码文件为do_nothing.cpp,可以使用 g++ -shared -fPIC -o do_nothing.so do_nothing.cpp 生成对应的动态链接库文件。在do_nothing.cpp文件中,需要根据具体需求实现TransferRecord函数。
TransferRecord函数示例
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <assert.h>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <tr1/functional>
#define TCAPLUS_MAX_TABLE_NAME_LEN_FOR_PLUGIN 128
enum EnumTransRecordStatus {
ENUM_TRANS_RECORD_SUC = 0,
ENUM_TRANS_RECORD_NOT_EXIST = 1,
ENUM_TRANS_RECORD_FAIL= -1,
};
typedef int int32_t;
typedef struct {
int32_t iAppID;
int32_t iZoneID;
char szTableName[TCAPLUS_MAX_TABLE_NAME_LEN_FOR_PLUGIN];
std::tr1::function<int(const char*)> pFuncForBillLog;
} TcapsvrImportCallBackPara;
typedef std::map<std::string, std::string> TcapFieldNameValueMap;
typedef std::vector<TcapFieldNameValueMap> VALUELIST;
typedef std::vector<EnumTransRecordStatus> STATUSLIST;
using namespace std;
extern "C" int TransferRecord(const VALUELIST& old_list,
VALUELIST& new_list, STATUSLIST& status_list,
TcapsvrImportCallBackPara* pCallBackPara)
{
if (NULL == pCallBackPara)
{
return -1;
}
VALUELIST::const_iterator it_list;
new_list.clear();
for (it_list = old_list.begin(); it_list != old_list.end(); ++it_list)
{
int ret = ENUM_TRANS_RECORD_SUC;
TcapFieldNameValueMap new_map;
const TcapFieldNameValueMap& old_map = *it_list;
TcapFieldNameValueMap::const_iterator it;
for (it = old_map.begin(); it != old_map.end(); ++it)
{
std::pair<TcapFieldNameValueMap::iterator, bool> result;
result = new_map.insert(std::make_pair(it->first, it->second));
if(!result.second)
{
status_list.push_back(ENUM_TRANS_RECORD_FAIL);
return -1;
}
}
// add new filed: platformid, use default value 0
// int32_t platformid = 0;
// std::string new_value((char*)&platformid, sizeof(platformid));
// std::string new_key = "platformid";
// std::pair<TcapFieldNameValueMap::iterator, bool> result = new_map.insert(std::make_pair(new_key, new_value));
// if(!result.second)
// {
// status_list.push_back(ENUM_TRANS_RECORD_FAIL);
// return -1;
// }
new_list.push_back(new_map);
status_list.push_back((EnumTransRecordStatus)ret);
}
return 0;
}
按照上述函数示例生成so文件,并在tcapulogmgr_cfg.xml中配置相关参数后,执行 ./tcapulogmgr updatefields --directory=/txh/ulog/1.2.2.1 --engine-directory=/txh --tbusid=1.2.2.1 --conf=tcapulogmgr_cfg.xml --seqcontinuous。假设/txh目录下由配置文件中SourceTableName字段指定的数据表的引擎文件的checkpoint为50,/txh/ulog/1.2.2.1目录下最大的sequence为60,那么该命令会redo sequence在51到60之间的binlog,对SourceTableName字段指定的数据表进行回档操作;同时,会使用so文件对这10条binlog对应的记录结构进行转换,将其redo到DestTableName字段指定的数据表中。